<?php

/**
 * 演示案例控制器
 * @author 邝达峰
 */
class Uploadimg extends BaseAction
{
	private $_uid=0;
	/**
	 * 初始化
	 */
	public function _init()
	{
		$strHost = '';
		$referer = $_SERVER["HTTP_REFERER"];
		$arrReferer = parse_url($referer);
		if($arrReferer){
			$strHost = $arrReferer['host'];
		}
		if($strHost=='system.gzcanr.com'){
			$this->_uid = $this->session()->getSession('ADMINID')-0;
		}else{
			$this->_uid = $this->session()->getSession('uid')-0;
			if(empty($this->_uid)) $this->_uid = $_COOKIE['uid']-0;
		}
	}

	/**
	 * 默认动作
	 */
	protected function _default()
	{
		// 指定允许其他域名访问
		header('Access-Control-Allow-Origin:*');  //header('Access-Control-Allow-Origin:http://www.gzcanr.com');
		// 响应类型
		header('Access-Control-Allow-Methods:POST');
		// 响应头设置
		header('Access-Control-Allow-Headers:x-requested-with,content-type');
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");

		@set_time_limit(0);

		if($_SERVER["REQUEST_METHOD"] == "POST") {
			$channel = $_POST['channel'];
			$dir = $_POST['dir'];
			$ip = getClientIp();
			$ip = $ip=='Unknown'?0:ip2long($ip);
			$max = intval($_POST['maxsize']);
			$max = $max*1024;
			
			#检查图片路径
			$checkPath = explode('/', trim($channel,'/'));
			$pathLen = count($checkPath);
			if($pathLen > 3){
			   $path_day = $checkPath[$pathLen-1];
			   $path_month = $checkPath[$pathLen-2];
			   $now_day = date("d");
			   $now_month = date("m");
			   if($path_day!=$now_day || $path_month!=$now_month){
			       exit('error');
			   }
			}
			
			if(!empty($_FILES['file']['name'])){
				$src = ''; $id=0;
				try {
					$oImage = new Upload($channel);
					/*
					if (!empty($dir)&&empty($channel)) {
					$oImage = new Upload($dir, $max);
					} else {
					$oImage = new Upload($channel);
					}
					*/
					$oImage->BUILD_IMG['file']['s'][] = array(100,100);
					$oImage->BUILD_IMG['file']['n'][] = 's_';
					$filepath = $oImage->upFiles();
					if ($oImage->error=='') {
						$src = Func::imageSrcHandle($filepath, 'z', '100*100');
					}
					die('{"src":"'.$src.'","error":"'.$oImage->error.'","id":'.$id.'}');
				}
				catch (Exception $e)
				{
					if($e->getMessage()){
						die('{"src":"'.$src.'","error":"'.'Image Server Error'.'","id":'.'0'.'}');
					}
					exit;
				}

			} else {
				die('{"src":"","error":"Please pick a Image","id":0}');
				exit;
			}
		} else {
			echo "<form action='' method='post' enctype='multipart/form-data'> <input type='file' name='file'/><input type='hidden' name='max_file_size' value='1'/><input type='hidden' name='dir' value='uploads/user/11/03/'/><input type='submit' value='submit'/></form>";
			exit;
		}
	}

	protected function _del()
	{
		$ip = getClientIp();
		$ip = $ip=='Unknown'?0:ip2long($ip);
		$oImage = new Upload('', 1);
		$filepath = $_POST['filepath'];
		$fileid = $_POST['id']-0;
		$oImage->del_file($filepath,1);
		$where = "id=".$fileid;
		$where .=' AND ip='.$ip;
		if(!empty($this->_uid))   {
			$where .=' AND user_id='.$this->_uid;
		}
		DB::delete("temp_fileupload", $where);
		exit('ok');
	}


}
?>